package com.ftg.learn.amqp.helloworld;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 布衣
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        //基础信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.2.40.82");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/demo");

        //拿到连接
        Connection connection = factory.newConnection();
        //创建channel
        Channel channel = connection.createChannel();

        //创建队列
        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, "myChart", "");

        //    通过频道绑定队列，


        //   接收数据
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
    }
}
